'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class MultiMergeXML
      Inherits MultiPass

#Region "Class level declarations"
      Dim mBaseDir As String
      Dim mBaseFilePattern As String
      Dim mMergingDir As String
      Dim mMergingFilePattern As String
#End Region

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal statusDirective As IStatusDirective)
         Dim multiPassDirective As CodeGenerationSupport.IMultiPassDirective
         Dim multiMergeXMLDirective As CodeGenerationSupport.IMultiMergeXMLDirective

         multiPassDirective = CType(node, CodeGenerationSupport.IMultiPassDirective)
         multiMergeXMLDirective = CType(node, CodeGenerationSupport.IMultiMergeXMLDirective)
         mBaseDir = multiMergeXMLDirective.BaseDir
         mBaseFilePattern = multiMergeXMLDirective.BaseFilePattern
         mMergingDir = multiMergeXMLDirective.MergingDir
         mMergingFilePattern = multiMergeXMLDirective.MergingFilePattern
         Try
            Me.MultiPass(multiPassDirective, _
                     Nothing, Nothing, _
                     AddressOf MultiMergeXMLCallBack, _
                     statusDirective, _
                     runUtility)
         Catch ex As System.Exception
            ' Exception should already be recorded
            'Diagnostics.Debug.WriteLine("")
         End Try
      End Sub

#End Region

      '#Region "Protected properties and methods"
      '#End Region

#Region "Private properties and methods"
      Private Function MultiMergeXMLCallBack( _
               ByVal outputFileName As String, _
               ByVal multiPassDirective As CodeGenerationSupport.IMultiPassDirective, _
               ByVal nodeSelect As Xml.XmlNode, _
               ByVal runUtility As CodeGenerationSupport.RunUtility, _
               ByVal statusOutput As CodeGenerationSupport.IStatusOutput) _
               As IO.Stream
         Dim baseFileName As String
         Dim mergeFileName As String
         Dim baseXMLDoc As New Xml.XmlDocument
         Dim mergeXMLDoc As New Xml.XmlDocument
         Dim outStream As New IO.MemoryStream
         ' Determine names based on nodeSelect
         baseFileName = IO.Path.Combine( _
                                 CodeGenerationSupport.RunUtility.ParseFilenamePattern(Me.mBaseDir, nodeSelect, NsMgrMeta), _
                                 CodeGenerationSupport.RunUtility.ParseFilenamePattern(Me.mBaseFilePattern, nodeSelect, NsMgrMeta))
         mergeFileName = IO.Path.Combine( _
                                 CodeGenerationSupport.RunUtility.ParseFilenamePattern(Me.mMergingDir, nodeSelect, NsMgrMeta), _
                                 CodeGenerationSupport.RunUtility.ParseFilenamePattern(Me.mMergingFilePattern, nodeSelect, NsMgrMeta))
         If IO.File.Exists(mergeFileName) And Not IO.File.Exists(baseFileName) Then
            ' Just use the merge file. While this isn't common, it isn't a crises
            baseXMLDoc.Load(mergeFileName)
         ElseIf IO.File.Exists(mergeFileName) And IO.File.Exists(baseFileName) Then
            baseXMLDoc.Load(baseFileName)
            mergeXMLDoc.Load(mergeFileName)
            Metadata.MergeFreeForm.Merge( _
                 baseXMLDoc, _
                 mergeXMLDoc)
         Else
            ' just use the base file, this is a common case.
            baseXMLDoc.Load(baseFileName)
         End If
         baseXMLDoc.Save(outStream)
         Return outStream
      End Function
#End Region
   End Class
End Namespace